//
//  CriterionView_Extras.m
//  funkydog
//
//  Created by Saptarshi Guha on 1/23/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "Criterion.h"
#import "CriteionView_MineView.h"
#import "CriterionView_Extras.h"
#import "FunkyDog.h"
#import <objc/objc.h>
#import <objc/objc-runtime.h>
#import <objc/objc-class.h>
struct menuItemInfo {
    int _field1;
    int _field2;
};

@implementation CriterionView(Extras)
static NSMapTable *_MYSimulatedIVarMapTable = NULL;
+ (NSMapTable *)_mySimulatedIVarMapTable
{
	if(NULL == _MYSimulatedIVarMapTable)
	{
		
		_MYSimulatedIVarMapTable = NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 16);
	}
	return _MYSimulatedIVarMapTable;	
}

- (void)mySetUserInfo:(NSDictionary *)aDictionary
{
	NSDictionary    *newDictionary = [aDictionary copy];
	NSMapInsert([[self class] _mySimulatedIVarMapTable], self, newDictionary);
	[newDictionary release];
}

- (NSDictionary *)myUserInfo
{
	return NSMapGet([[self class] _mySimulatedIVarMapTable], self);
}

+(void)load{
	if (self == [CriterionView class]){
		Method originalMethod ,replacedMethod;
		originalMethod  = class_getInstanceMethod(self, @selector(configureViewForQualifierTag:criterion:));
		replacedMethod  = class_getInstanceMethod(self, @selector(myconfigureViewForQualifierTag:criterion:));
		method_exchangeImplementations(originalMethod, replacedMethod);
		originalMethod  = class_getInstanceMethod(self, @selector(awakeFromNib));
		replacedMethod  = class_getInstanceMethod(self, @selector(myawakeFromNib));
		method_exchangeImplementations(originalMethod, replacedMethod);
		//originalMethod  = class_getInstanceMethod(self, @selector(putHeaderCriteriaInPopUpMenu));
		//replacedMethod  = class_getInstanceMethod(self, @selector(myputHeaderCriteriaInPopUpMenu));
		//method_exchangeImplementations(originalMethod, replacedMethod);
	//	originalMethod  = class_getInstanceMethod(self, @selector(configureForCriterion:));
//		replacedMethod  = class_getInstanceMethod(self, @selector(myconfigureForCriterion:));
//		method_exchangeImplementations(originalMethod, replacedMethod);
		originalMethod  = class_getInstanceMethod(self, @selector(criterionPopupChanged:));
		replacedMethod  = class_getInstanceMethod(self, @selector(mycriterionPopupChanged:));
		method_exchangeImplementations(originalMethod, replacedMethod);
		originalMethod  = class_getInstanceMethod(self, @selector(qualifierPopupChanged:));
		replacedMethod  = class_getInstanceMethod(self, @selector(myqualifierPopupChanged:));
		method_exchangeImplementations(originalMethod, replacedMethod);
		originalMethod  = class_getInstanceMethod(self, @selector(criterion));
		replacedMethod  = class_getInstanceMethod(self, @selector(mycriterion));
		method_exchangeImplementations(originalMethod, replacedMethod);
		originalMethod  = class_getInstanceMethod(self, @selector(menuUsingInfo:length:));
		replacedMethod  = class_getInstanceMethod(self, @selector(mymenuUsingInfo:length:));
		method_exchangeImplementations(originalMethod, replacedMethod);
		originalMethod  = class_getInstanceMethod(self, @selector(dealloc));
		replacedMethod  = class_getInstanceMethod(self, @selector(mydealloc));
		method_exchangeImplementations(originalMethod, replacedMethod);
		
	}
}
-(void)mydealloc{
	NSMapRemove([[self class] _mySimulatedIVarMapTable], self);
	[self mydealloc];
}
-(id)mycriterion{
	Criterion *x=[self mycriterion];
	if ( [ [[criterionPopUp selectedItem] title] isEqualToString:@"Tags"]){
		[x setCriterionIdentifier:TAGID];
	}
	if ( [ [[criterionPopUp selectedItem] title] isEqualToString:@"Notes"]){
		[x setCriterionIdentifier:NOTEID];
	}
	if ( [ [[criterionPopUp selectedItem] title] isEqualToString:@"Steroids"]){
		[x setCriterionIdentifier:STEROIDID];
	}
	return x;
}
-(id)mymenuUsingInfo:(struct menuItemInfo *)fp8 length:(int)fp12{
	NSMenu *x=[self mymenuUsingInfo:fp8 length:fp12];
	if(fp12>20){ //so crass
		[x addItem:[NSMenuItem separatorItem]];
		[x addItemWithTitle:@"Tags" action:Nil keyEquivalent:@""];
		[x addItemWithTitle:@"Notes" action:Nil keyEquivalent:@""];
		[x addItemWithTitle:@"Steroids" action:Nil keyEquivalent:@""];
	}
	return x;
}
-(void)replaceoldviewswithTagNoteViews{
	NSArray *va=[self subviews];
	NSView *vv=[va objectAtIndex:2];
	CriteionView_MineView *cvm=(CriteionView_MineView*)[[self myUserInfo] objectForKey:[self description]];
	[[cvm extraview] setFrame:[vv frame]];
	[self replaceSubview:vv with:[cvm extraview]];
	currentView=[cvm extraview];
	[[cvm extraquals] setTarget:self];
	[[cvm extraquals] setAction:@selector(qualifierPopupChanged:)];
	[[cvm extracris] setTarget:self];
	[[cvm extracris] setAction:@selector(criterionPopupChanged:)];
	[[cvm extracris] setMenu:[criterionPopUp menu]];
	criterionPopUp=[cvm extracris];
	qualifierPopUp=[cvm extraquals];
	expressionStringField=[cvm extraexpression];
}
-(void)replaceoldviewswithSteroidViews{
	NSArray *va=[self subviews];
	NSView *vv=[va objectAtIndex:2];
	CriteionView_MineView *cvm=(CriteionView_MineView*)[[self myUserInfo] objectForKey:[self description]];
	[[cvm steroidview] setFrame:[vv frame]];
	[self replaceSubview:vv with:[cvm steroidview]];
	currentView=[cvm steroidview];
	[[cvm steroidcris] setTarget:self];
	[[cvm steroidcris] setAction:@selector(criterionPopupChanged:)];
	[[cvm steroidcris] setMenu:[criterionPopUp menu]];
	criterionPopUp=[cvm steroidcris];
	expressionStringField=[cvm steroidexpression];
}

-(void)myqualifierPopupChanged:(id)fp8{
	if (!( [[[criterionPopUp selectedItem] title] isEqualToString:@"Tags"]
		  || [[[criterionPopUp selectedItem] title] isEqualToString:@"Notes"])) [self myqualifierPopupChanged:fp8];
}
		
-(void)mycriterionPopupChanged:(id)fp8{
	
	[self mycriterionPopupChanged:fp8];
	if([ [fp8 titleOfSelectedItem] isEqualToString:@"Tags"] || [ [fp8 titleOfSelectedItem] isEqualToString:@"Notes"]){
		[self replaceoldviewswithTagNoteViews];
	}else
		if([ [fp8 titleOfSelectedItem] isEqualToString:@"Steroids"]){
			[self replaceoldviewswithSteroidViews];
		}	
}
//Following is called only once when the view is created either by editing smart mailbox or adding another criterion.
//-(void)myconfigureForCriterion:(id)fp8{
//	[self myconfigureForCriterion:fp8];
//	
//}//=
//-(void)myputHeaderCriteriaInPopUpMenu{
//	[self myputHeaderCriteriaInPopUpMenu];
//		
//	NSLog(@"CriterionView putHeaderCriteriaInPopUpMenu]");
//}
-(void)myawakeFromNib{
	[self myawakeFromNib];
	CriteionView_MineView *cvm=[[CriteionView_MineView alloc] init];
	[self mySetUserInfo:[ NSDictionary dictionaryWithObject:cvm forKey:[self description]]];
	
}

- (void)myconfigureViewForQualifierTag:(int)fp8 criterion:(id)fp12{
	
	[self myconfigureViewForQualifierTag:fp8 criterion:fp12];
	if([[(Criterion*)fp12 criterionIdentifier] isEqualToString:TAGID]) {
		[criterionPopUp selectItemWithTitle:@"Tags"];
		[self replaceoldviewswithTagNoteViews];
		expressionGuess=[fp12 expression];
		[expressionStringField setStringValue:expressionGuess];
	}
	else	
	if([[(Criterion*)fp12 criterionIdentifier] isEqualToString:NOTEID]) {
		[criterionPopUp selectItemWithTitle:@"Notes"];
		[self replaceoldviewswithTagNoteViews];
		expressionGuess=[fp12 expression];
		[expressionStringField setStringValue:expressionGuess];
		}
	else
	if([[(Criterion*)fp12 criterionIdentifier] isEqualToString:STEROIDID]) {
		[criterionPopUp selectItemWithTitle:@"Steroids"];
		[self replaceoldviewswithSteroidViews];
		expressionGuess=[fp12 expression];
		[expressionStringField setStringValue:expressionGuess];
		}
}
@end
